package cn.itcast.service.impl;

import cn.itcast.domain.Dept;
import cn.itcast.factory.MapperFactory;
import cn.itcast.mapper.CompanyMappe;
import cn.itcast.mapper.DeptMapper;
import cn.itcast.service.DeptService;
import cn.itcast.utils.TransactionUtil;
import cn.itcast.utils.UuidUtils;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class DeptServiceImpl implements DeptService {
    //查询所有
    public List<Dept> findAll(int pageNum) {

        //获取session
        SqlSession sqlSession = MapperFactory.getSqlSession();
        //获取mapper对象
        DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
        //分页
        PageHelper.startPage(pageNum,5);
        List<Dept> deptList =  mapper.findAll();
        TransactionUtil.close(sqlSession);
        return deptList;
    }

    @Override
    public void add(Dept dept) {
        //获取session
        SqlSession sqlSession = MapperFactory.getSqlSession();
        try {
            //获取mapper对象
            DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
            String uuid = UuidUtils.getUuid();
            dept.setId(uuid);
            mapper.add(dept);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }

    }

    @Override
    public List<Dept> findAll() {
        //获取session
        SqlSession sqlSession = MapperFactory.getSqlSession();
        //获取mapper对象
        DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);

        List<Dept> deptList =  mapper.findAll();
        TransactionUtil.close(sqlSession);
        return deptList;

    }

    //根据id查询
    public Dept findById(String id) {
        //获取session
        SqlSession sqlSession = MapperFactory.getSqlSession();
        //获取mapper对象
        DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);

        Dept dept = mapper.findById(id);

        TransactionUtil.close(sqlSession);
        return dept;
    }

    //修改
    public void update(Dept dept) {
        //获取session
        SqlSession sqlSession = MapperFactory.getSqlSession();
        try {
            //获取mapper对象
            DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
            mapper.edit(dept);
            TransactionUtil.commit(sqlSession);
        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            e.printStackTrace();
        } finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public void deleteByIds(String ids) {
        if (ids != null && ids.length() > 0) {

            String[] id = ids.split(",");

            //获取session
            SqlSession sqlSession = MapperFactory.getSqlSession();
            try {
                //获取mapper对象
                DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
                mapper.deleteByIds(id);
                TransactionUtil.commit(sqlSession);
            } catch (Exception e) {
                TransactionUtil.rollback(sqlSession);
                e.printStackTrace();
            } finally {
                TransactionUtil.close(sqlSession);
            }

        }
    }
}
